<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="w-1/4">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/助记码/姓名/助记码/手机号" allow-clear />
        </a-form-item>
        <a-form-item name="dateRange" label="退药日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <a-button type="primary" ghost @click="onPrint"><BaseIcon name="PrinterOutlined" :loading="printLoading" />退药单</a-button>
        <!-- <BaseExport type="门诊" name="西成药退药查询" code="000355" :search-params="formState">导出</BaseExport> -->
      </div>
      <BaseTable :loading="loading" column-code="000355" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg }" />
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const formState = ref({
  zjm: '',
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 10)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zyyf/getStatisticsDistributeDrugTyList',
  params: formState.value,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      zjm: formState.value.zjm
    }
  }
})
const printLoading = ref(false)
// 打印退药单
const onPrint = async () => {
  printLoading.value = true
  const res = await post('/inp/Print/printAfterTYD', {
    stdate: formState.value['dateRange']?.[0],
    eddate: formState.value['dateRange']?.[1],
    zjm: formState.value.zjm
  })
  printLoading.value = false
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  window.openExe?.rpt(res.data) //调用winform打印
}
onMounted(() => {
  getData()
})
</script>
